package com.herui.iot.ui.mime;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.ui.BaseSportActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class UserChangePhoneActivity extends BaseSportActivity {

    private UserPresenter userPresenter;
    private int getCodeFlag;

    @BindView(R.id.et_phone)
    EditText phone;
    @BindView(R.id.et_code)
    EditText code;
    @BindView(R.id.view_get_code_fail)
    LinearLayout getCodeFail;
    @BindView(R.id.view_get_code_success)
    LinearLayout getCodeSuccess;
    @BindView(R.id.btn_get_code)
    Button getCode;
    @BindView(R.id.btn_confirm)
    Button confirm;
    @BindView(R.id.btn_resend_get_code)
    Button resendGetCode;
    @BindView(R.id.view_code_fail)
    LinearLayout viewCodeFail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("修改手机号码");
        userPresenter = new UserPresenter(userView);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_user_change_phone;
    }

    @Override
    public void initParam() {

    }

    @OnClick({R.id.btn_get_code,R.id.btn_confirm,R.id.btn_resend_get_code})
    public void onViewClick(View view) {
        String s = phone.getText().toString();
        switch (view.getId()){
            case R.id.btn_get_code:
                getCodeFlag = 0;
                if(s.equals("")){
                    getCodeSuccess.setVisibility(View.GONE);
                    getCodeFail.setVisibility(View.VISIBLE);
                }else{
                    getCodeSuccess.setVisibility(View.GONE);
                    getCodeFail.setVisibility(View.GONE);
                    userPresenter.getCode(s);
                }
                break;
            case R.id.btn_confirm:
                String c = code.getText().toString();
                userPresenter.modifyPhone(s,c);
                break;
            case R.id.btn_resend_get_code:
                getCodeSuccess.setVisibility(View.GONE);
                getCodeFail.setVisibility(View.GONE);
                viewCodeFail.setVisibility(View.GONE);
                userPresenter.getCode(s);
                getCodeFlag = 1;
                break;
            default:
                break;
        }
    }

    UserPresenter.UserView userView = new UserPresenter.UserView() {
        @Override
        public void onCode(boolean flag) {
            if(flag){
                getCodeSuccess.setVisibility(View.VISIBLE);
                getCodeFail.setVisibility(View.GONE);
                getCode.setVisibility(View.GONE);
                confirm.setVisibility(View.VISIBLE);
                resendGetCode.setVisibility(View.VISIBLE);
            }else{
                Log.i("getCodeFlag","getCodeFlag=" + getCodeFlag);
                if(getCodeFlag == 0){
                    getCodeSuccess.setVisibility(View.GONE);
                    getCodeFail.setVisibility(View.VISIBLE);
                }
            }
        }

        @Override
        public void onModifyPhone(boolean flag) {
            if(flag){
                viewCodeFail.setVisibility(View.GONE);
                ToastUtil.showMessage("手机号修改成功");
                onBackPressed();
            }else{
                viewCodeFail.setVisibility(View.VISIBLE);
            }
        }
    };
}